/*
 * PasswordDialog.java
 *
 * Created on July 11, 2008, 2:32 PM
 */

package org.potomak.patientmanager.gui;

import org.potomak.patientmanager.persistence.JPoxJDO;
import org.potomak.patientmanager.tools.PasswordMD5;
import org.potomak.patientmanager.ContactsProperties;

import javax.swing.JFrame;

/**
 *
 * @author  mobile
 */
public class PasswordDialog extends javax.swing.JDialog {

    /** Creates new form PasswordDialog */
    public PasswordDialog(java.awt.Frame parent) {
        super(parent);
        
        setResizable(false);
        
        parent.setEnabled(false);
        parent.setFocusableWindowState(false);

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        
        initComponents();
        
        getRootPane().setDefaultButton(okButton);
    }
    
    private void formWindowClosing(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        okButton = new javax.swing.JButton();
        passwordLabel = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        okButton.setText("Ok");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        passwordLabel.setText("Password");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(okButton)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(passwordLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 45, Short.MAX_VALUE)
                        .add(passwordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 173, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(passwordLabel)
                    .add(passwordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(okButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
    setVisible(false);
    
    JPoxJDO jpoxJDO = null;
    try {
        if(ContactsProperties.getProperty("DEBUG")) {
            System.out.println("Password ---> "+PasswordMD5.passwordMD5(new String(passwordField.getPassword())));
        }
        jpoxJDO = new JPoxJDO(PasswordMD5.passwordMD5(new String(passwordField.getPassword())));
    }
    catch(Exception e) {
        System.out.println("Eccezione ---> "+e);
    }
    
    if(jpoxJDO != null) {
        ((Contacts) this.getParent()).setJpoxJDO(jpoxJDO);
        ((Contacts) this.getParent()).updatePatientListData();
        this.getParent().setEnabled(true);
        ((JFrame) this.getParent()).setFocusableWindowState(true);
    }
    else {
        System.exit(0);
    }
}//GEN-LAST:event_okButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton okButton;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JLabel passwordLabel;
    // End of variables declaration//GEN-END:variables

}
